package com.ftwj.demo.base.util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public abstract class MD5 {

	/**
	 * 	对字符串进行MD5加密
	 * 
	 * @param s 要加密的字符串
	 * @return md5加密后的字符串
	 */
	public static String encode(String str) {
		byte[] unencoded = str.getBytes();
		byte[] encoded = {};
		try {
			MessageDigest md = MessageDigest.getInstance("MD5");
			md.reset();
			md.update(unencoded);
			encoded=md.digest();
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}

		StringBuffer buf = new StringBuffer();
		for (int i = 0; i < encoded.length; i++) {
			if ((encoded[i] & 0xFF) < 16) {
				buf.append("0");
			}
			buf.append(Long.toString(encoded[i] & 0xFF, 16));
		}
		return buf.toString();
	}

}
